package com.haiqiu.tool.excel.controller;

import com.haiqiu.tool.excel.utils.AutoNameUtil;
import com.haiqiu.tool.excel.utils.HqExcelUtils;
import com.haiqiu.tool.excel.entity.Person;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@RestController
@RequestMapping
public class HqExcelController {


    @GetMapping("test")
    public String export() {
        Random random = new Random();
        List<Person> personList = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            Person person = new Person();
            person.setName(AutoNameUtil.autoSurAndName());
            person.setId(random.nextInt(8000));
            person.setAddress(AutoNameUtil.autoSurAndName());
            person.setAge(random.nextInt(100));
            person.setPrice((float) 200.23 + i);
            person.setBirthday(new Date());
            person.setStatus(random.nextInt(3));
            personList.add(person);
        }
        long start = System.currentTimeMillis();
        HqExcelUtils.getListToExcel(Person.class, personList, "人员信息");
        long end = System.currentTimeMillis();
        long consume = end - start;
        System.out.println("单线程---------导出完成------------------耗时：" + consume);
        return "成功";
    }

    @GetMapping("test1")
    public String export1() {
        List<Map<String, Object>> personList = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("姓名", "小海+i");
            map.put("ID", i);
            Random random = new Random();
            map.put("地址", "春熙成华大道" + random.nextInt(99));
            map.put("年龄", 10 + i);
            map.put("价格", (float) 200.23 + i);
            map.put("生日", new Date());
            personList.add(map);
        }
        HqExcelUtils.getListToExcel(personList, "人员信息");
        return "成功";
    }

    @PostMapping("/import")
    public List<Person> importExcel(@RequestParam(name = "file") MultipartFile multipartFile) {
        return HqExcelUtils.getExcelToList("用户管理", multipartFile, Person.class);
    }

    @PostMapping("/import1")
    public List<Map<String, Object>> importExcel1(@RequestParam(name = "file") MultipartFile multipartFile) {
        return HqExcelUtils.getExcelToMaps("用户管理", multipartFile);
    }

    @GetMapping("/template")
    public String template() {
        HqExcelUtils.createTemplate("用户列表", "用户管理", Person.class);
        return "成功";
    }

    @GetMapping("/template1")
    public String template1() {
        Person person = new Person(1, "海秋", 18, "中国大陆", (float) 18.99, new Date(), 1);
        HqExcelUtils.createTemplate("用户列表", "用户管理", Person.class, Collections.singletonList(person));
        return "成功";
    }


}
